package org.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.pojo.Headline;
import org.example.service.HeadlineService;
import org.example.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 宋孟林
 * @description
 */
@RestController
@RequestMapping("/headline")
@Slf4j
public class HeadlineController {
    @Autowired
    private HeadlineService headlineService;
    @PostMapping("/publish")
    public Result<Object> publish(@RequestBody Headline headline, @RequestHeader String token){
        log.info("插入头条为对象为： {}", headline);
        return headlineService.publish(headline, token);
    }

    @PostMapping("/findHeadlineByHid")
    public Result<Object> findHeadlineByHid(@RequestParam int hid, @RequestHeader String token){
        return headlineService.findHeadlineByHid(hid, token);
    }

    @PostMapping("/update")
    public Result<Object> update(@RequestBody Headline headline, @RequestHeader String token){
        return headlineService.CustomUpdate(headline, token);
    }

    @PostMapping("/removeByHid")
    public Result<Object> delete(@RequestParam int hid){
        boolean flag = headlineService.removeById(hid);;
        if (flag){
            return Result.ok(null);
        }else
            return Result.build(null, 500, "删除失败");
    }
}
